
/* --------------------------------------- Include Files ---------------------------------------- */
#include "common.h"
#include "stm32f10x.h"
#include "gpio.h"

/* ---------------------------------------- Macro Defines --------------------------------------- */


/* ----------------------------------- --- Datatype Defines --------------------------------------- */
/* IO Port Data Type Define */
typedef struct
{
	uint32_t         pin_id;
	GPIO_TypeDef*  gpio_port;
	uint16_t         gpio_pin;
}DIO_IO_PORT;

/* --------------------------------- Global Function Prototypes --------------------------------- */
void Dio_initModule(void);
uint32_t Dio_readPinValue(uint32_t pin_id, uint32_t *pin_value);
uint32_t Dio_writePinValue(uint32_t pin_id, uint32_t pin_value);

/* -------------------------------------- Static Variables -------------------------------------- */
/* Input Port Signal & Pin Mapping   */
static DIO_IO_PORT dio_input_signal[3] = 
{
	{0,   GPIOB,   GPIO_Pin_8},			//onlock
	{1,   GPIOB,   GPIO_Pin_9},			//offlock
	{2,	  GPIOB,   GPIO_Pin_5}			//bolt
};

/* Output Port Signal & Pin Mapping */
static DIO_IO_PORT dio_output_signal[2] =
{
	{0,  GPIOA,  GPIO_Pin_11},
	{1,  GPIOA,  GPIO_Pin_12}
};


/*************************************************************************************************
 * Procedure: Dio_initModule
 * Object: 
 * Parameters In:
 * - none
 * Parameters Out:
 * - none
 *************************************************************************************************/
void Dio_initModule(void)
{
  // TODO
}

/**
  * @brief  Dio_readPinValue 
  * @param 
  * @retval
  * @brief
  */
uint32_t Dio_readPinValue(uint32_t pin_id, uint32_t* pin_value)
{
    uint32_t temp = 0;
    
    /* is pin_id out of range */
    if(pin_id > 3){
        return (0x00);
    }

    /* Read Pin Value */
    temp = (dio_input_signal[pin_id].gpio_pin)&(GPIO_ReadInputData(dio_input_signal[pin_id].gpio_port));
    
    /* Return Pin Value */
    if(temp == 0){
        *pin_value = 0x00;
    }
    else{
        *pin_value = 0x01;
    }
    
    return 0x01;
}


/**
  * @brief  Dio_writePinValue 
  * @param 
	* 				- pin_id:      output pin parinfo
	* 				- pin_value:   output pin value, 0x01 /0x00
  * @retval
  * @brief
  */
uint32_t Dio_writePinValue(uint32_t pin_id, uint32_t pin_value)
{
    /* is pin parinfo out of range */
    if(pin_id > 2){
        return 0x00;
    }
    
    /* Control output pin high or low */
    if(pin_value == 0x01){
        GPIO_SetBits(dio_output_signal[pin_id].gpio_port, dio_output_signal[pin_id].gpio_pin);
        return 0x01;
    }
    else if(pin_value == 0x00){
         GPIO_ResetBits(dio_output_signal[pin_id].gpio_port, dio_output_signal[pin_id].gpio_pin);
         return 0x01;        
    }
    else{
        return 0x00;
    }
}
